package com.example.pc.mzbannerview;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import com.example.pc.mzbannerview.constant.Constant;
import com.example.pc.mzbannerview.utils.GlideUtils;
import com.example.pc.mzbannerview.utils.OkHttpUtils;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private MZBannerView mMZBanner;
    private List<String> mList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
    }

    private void initView() {
        mMZBanner = findViewById(R.id.banner);
        mMZBanner.setIndicatorVisible(false);
    }

    private void initData() {

        OkHttpUtils.get(Constant.BANNER_URL, new OkHttpUtils.OnOkHttpCallback() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onFailure(String msg) {

            }

            @Override
            public void onSuccessful(String json) {
                if (!TextUtils.isEmpty(json)){
                    postJson(json);
                }
            }
        });
    }

    private void postJson(String json) {
        try {
            JSONObject jsonObject=new JSONObject(json);
            JSONObject jsonData=jsonObject.getJSONObject("data");
            JSONArray jsonArray=jsonData.getJSONArray("banner");
            for (int i=0;i<jsonArray.length();i++){
                JSONObject jsonCover= (JSONObject) jsonArray.get(i);
                String cover=jsonCover.getString("cover");
                mList.add(cover);

                initSetData();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void initSetData() {
        // 设置数据
        mMZBanner.setPages(mList, new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });

        mMZBanner.start();
    }

    public static class BannerViewHolder implements MZViewHolder<String> {
        private ImageView mImageView;
        @Override
        public View createView(Context context) {
            // 返回页面布局
            View view = LayoutInflater.from(context).inflate(R.layout.banner_item,null);
            mImageView = view.findViewById(R.id.banner_image);
            return view;
        }

        @Override
        public void onBind(Context context, int position, String data) {
            // 数据绑定
            GlideUtils.loadImageviewCenterCrop(context,data,mImageView);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        mMZBanner.pause();//暂停轮播
    }

    @Override
    public void onResume() {
        super.onResume();
        mMZBanner.start();//开始轮播
    }
}
